package com.zzjson.provider;

import com.zzjson.RpcRequest;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Map;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.provider</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月23日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class ProcessorHandler implements Runnable {
	private Socket socket;


	// private Object service;

	private Map<String, Object> handlerMap;

/*	public ProcessorHandler(Socket socket, Object service) {
		this.socket = socket;
		this.service = service;
	}*/

	public ProcessorHandler(Socket socket, Map<String, Object> handlerMap) {
		this.socket = socket;
		this.handlerMap = handlerMap;
	}

	@Override
	public void run() {
		ObjectInputStream objectInputStream = null;

		ObjectOutputStream objectOutputStream = null;

		try {
			objectInputStream = new ObjectInputStream(socket.getInputStream());
			//	输入流中需要有请求类，请求方法名称，请求参数

			RpcRequest rpcRequest = (RpcRequest) objectInputStream.readObject();
			Object result = null;
			result = invoke(rpcRequest);
			objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
			objectOutputStream.writeObject(result);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} finally {
			if (objectInputStream != null) {
				try {
					objectInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}


	private Object invoke(RpcRequest rpcRequest) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

		String className = rpcRequest.getClassName();
		String version = rpcRequest.getVersion();
		if (!StringUtils.isEmpty(version)) {
			className += "-" + version;
		}
		Object service = handlerMap.get(className);
		if (service == null) {
			throw new RuntimeException("service not found:" + service);
		}


		//获取客户端请求的参数
		Object[] args = rpcRequest.getParameters();
		if (args != null) {

			//获取每个参数的类型
			Class[] parameterTypes = new Class[args.length];
			for (int i = 0; i < args.length; i++) {
				parameterTypes[i] = args[i].getClass();
			}
			//加载类
			Class<?> aClass = Class.forName(rpcRequest.getClassName());
			Method method = aClass.getMethod(rpcRequest.getMethodName(), parameterTypes);
			return method.invoke(service, args);
		} else {
			//加载类
			Class<?> aClass = Class.forName(rpcRequest.getClassName());
			Method method = aClass.getMethod(rpcRequest.getMethodName());
			return method.invoke(service);
		}


	}
}
